﻿using AutoMapper;
using Benefactor.Mappings;
using Benefactor.Models.DtoModels;
using Benefactor.Services;
using Benefactor.Services.Repositories;
using Benefactor.Validations.DtoValidations;
using Bigway.Common.Interfaces;
using FluentValidation;

namespace Benefactor;

public static class ServicesRegistry
{
    public static void AddGeneralServices(this IServiceCollection services, IConfiguration configuration)
    {
        var appConfig = new AppConfig(configuration);
        Statics.AppConfig = appConfig;
        services.AddSingleton<IAppConfig>(appConfig);

        var crembConnectionConfig = new CrmebConnectionConfig
        {
            ConnectionString = appConfig.CrmebDbConnectionString
        };
        services.AddSingleton(crembConnectionConfig);

        services.AddScoped<IProductTypeRepository, ProductTypeRepository>();
        services.AddScoped<IProductRepository, ProductRepository>();
        services.AddScoped<IBenefitsCardRepository, BenefitsCardRepository>();
        services.AddScoped<IBenefitsCardOrderRepository, BenefitsCardOrderRepository>();
        services.AddScoped<IBenefitsCardOrderStatusRepository, BenefitsCardOrderStatusRepository>();
        services.AddScoped<IIssuedBenefitsCardRepository, IssuedBenefitsCardRepository>();
        services.AddScoped<IBenefitRepository, BenefitRepository>();
        services.AddScoped<IVoucherRepository, VoucherRepository>();
        services.AddScoped<IRefererCommissionRepository, RefererCommissionRepository>();
        services.AddScoped<IRefererCommissionRecordRepository, RefererCommissionRecordRepository>();
        services.AddScoped<IRefererCommissionWithdrawalRecordRepository, RefererCommissionWithdrawalRecordRepository>();

        services.AddScoped<ICrmebProductRepository, CrmebProductRepository>();
        services.AddScoped<ICrmebOrderRepository, CrmebOrderRepository>();
        services.AddScoped<ICrmebOrderDetailRepository, CrmebOrderDetailRepository>();

        services.AddScoped<IBenefitsCardService, BenefitsCardService>();
        services.AddScoped<ICardNoGenerator, CardNoGenerator>();

        services.AddAsyncInitializer<Initializer>();
        var mapperConfiguration = new MapperConfiguration(cfg => {
            cfg.AddProfile<MapProfile>();
        });
        var mapper = mapperConfiguration.CreateMapper();
        services.AddSingleton(mapper);
    }

    public static void AddValidators(this IServiceCollection services)
    {
        services
            .AddScoped<IValidator<AddRefererCommissionWithdrawalRequestDto>,
                AddRefererCommissionWithdrawalRequestDtoValidator>();

        services
            .AddScoped<IValidator<IssueBenefitsCardThenAddOrderRequestDto>,
                IssueBenefitsCardThenAddOrderRequestDtoValidator>();
    }
}